package com.guyan.controller;

import com.guyan.vo.ResultVo;
import com.guyan.vo.WangEditorItemVo;
import com.guyan.vo.WangEditorVo;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * ClassName： ImageUploadController
 * Date： 2021/6/2 15:27
 *
 * @author 孤焰
 * @version 1.0
 **/

@Controller
@ResponseBody
@RequestMapping("/upload")
public class ImageUploadController {

    @PostMapping("/layuiUpload")
    public ResultVo layuiUpload(MultipartFile file, HttpServletRequest request) {
        if (file == null) {
            return ResultVo.error(5001, "文件未选择");
        }

        String srcFilePathStr = request.getServletContext().getRealPath("/layuiImg/");
        File srcFilePath = new File(srcFilePathStr);
        // 如果目标目录不存在则创建
        if (!srcFilePath.exists()) {
            srcFilePath.mkdir();
        }
        // 源文件名字
        String originalFilename = file.getOriginalFilename();
        // 服务器中的文件名字
        String filename = UUID.randomUUID().toString() +
                originalFilename.substring(originalFilename.lastIndexOf("."));

        try {
            file.transferTo(new File(srcFilePathStr + filename));
        } catch (IOException e) {
            e.printStackTrace();
            return ResultVo.error(5002, "磁盘IO异常");
        }

        //上传图片时自动备份
//        try {
//            FileUtils.copyFileToDirectory(new File(srcFilePathStr + filename),
//                    new File("D:\\Environment\\Apache Tomcat\\layui-img备份"));
//        } catch (IOException e) {
//            e.printStackTrace();
//            return ResultVo.error(5002, "自动备份时磁盘IO异常");
//        }

        return ResultVo.success("/layuiImg/" + filename);
    }

    @PostMapping("/wangEditorUpload")
    public WangEditorVo wangEditorUpload(MultipartFile[] files, HttpServletRequest request) {
        if (files == null) {
            return WangEditorVo.error(5001);
        }

        String srcFilePathStr = request.getServletContext().getRealPath("/wangEditorImg/");
        File srcFilePath = new File(srcFilePathStr);
        // 如果目标目录不存在则创建
        if (!srcFilePath.exists()) {
            srcFilePath.mkdir();
        }

        List<WangEditorItemVo> wangEditorItemVoList = new ArrayList<>();
        for (MultipartFile file : files) {
            // 源文件名字
            String originalFilename = file.getOriginalFilename();
            // 服务器中的文件名字
            String filename = UUID.randomUUID().toString() +
                    originalFilename.substring(originalFilename.lastIndexOf("."));

            try {
                file.transferTo(new File(srcFilePathStr + filename));
                wangEditorItemVoList.add(new WangEditorItemVo(
                        "/wangEditorImg/" + filename, "", ""));
            } catch (IOException e) {
                e.printStackTrace();
                return WangEditorVo.error(5002);
            }

            //上传图片时自动备份
//            try {
//                FileUtils.copyFileToDirectory(new File(srcFilePathStr + filename),
//                        new File("D:\\Environment\\Apache Tomcat\\wangEdior-img备份"));
//            } catch (IOException e) {
//                e.printStackTrace();
//                return WangEditorVo.error(5002);
//            }
        }

        return WangEditorVo.success(wangEditorItemVoList);
    }
}
